#include <iostream>
#include <vector>
#include <string>
using namespace std;

int min_match(string &s, int l, int r, vector<vector<int>> &dp)
{
	if (dp[l][r] != -1) {
		return dp[l][r];
	}
	if (l == r) {
		return dp[l][r] = 1;
	}

	if (l + 1 == r) {
		if ((s[l] == '(' && s[r] == ')') || (s[l] == '[' && s[r] == ']')) {
			return dp[l][r] = 0;
		} else {
			return dp[l][r] = 2;
		}
	}

	int p1 = INT32_MAX;
	if ((s[l] == '(' && s[r] == ')') || (s[l] == '[' && s[r] == ']')) {
		p1 = min_match(s, l + 1, r - 1, dp);
	}

	int ans = INT32_MAX;
	for (int m = l; m < r; m++) {
		ans = min(ans, min_match(s, l, m, dp) + min_match(s, m + 1, r, dp));
	}
	return dp[l][r] = min(ans, p1);
}

int main()
{
	string s = "(])])([]";
	int n = s.size();
	vector<vector<int>> dp(n, vector<int>(n, -1));
	cout << min_match(s, 0, n - 1, dp) << endl;
	return 0;
}